/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package quests.Q024_InhabitantsOfTheForestOfTheDead;

import java.util.Collection;

import net.sf.l2j.gameserver.model.L2Object;
import net.sf.l2j.gameserver.model.actor.L2Attackable;
import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;
import net.sf.l2j.gameserver.network.serverpackets.NpcSay;

public class Q024_InhabitantsOfTheForestOfTheDead extends Quest
{
	private final static String qn = "Q024_InhabitantsOfTheForestOfTheDead";
	
	//NPCs
	private static final int Dorian = 31389;
	private static final int Wizard = 31522;
	private static final int Tombstone = 31531;
	private static final int MaidOfLidia = 31532;
	
	//Items
	private static final int Letter = 7065;
	private static final int Hairpin = 7148;
	private static final int Totem = 7151;
	private static final int Flower = 7152;
	private static final int SilverCross = 7153;
	private static final int BrokenSilverCross = 7154;
	private static final int SuspiciousTotem = 7156;
	
	//Mobs
	private static final int mobs[] = { 21557, 21558, 21560, 21563, 21564, 21565, 21566, 21567 };
	
	private void autoChat(L2Npc npc, String text)
	{
		Collection<L2Object> objs = npc.getKnownList().getKnownObjects().values();
		for (L2Object obj : objs)
        {
			if (!(obj instanceof L2PcInstance))
            	continue;
            
            L2PcInstance target = (L2PcInstance) obj;
            
            target.broadcastPacket(new NpcSay(npc.getObjectId(), 0, npc.getNpcId(), text));
        }
	}
	
	public Q024_InhabitantsOfTheForestOfTheDead(int questId, String name, String descr)
	{
		super(questId,name,descr);
		
		questItemIds = new int[] { Flower, SilverCross, BrokenSilverCross, Letter, Hairpin, Totem };
		
		addStartNpc(Dorian);
		addTalkId(Dorian, Wizard, Tombstone, MaidOfLidia);
		
		addAggroRangeEnterId(25332);
		
		for(int mob : mobs)
			addKillId(mob);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("31389-03.htm")) //02
		{
			st.giveItems(Flower, 1);
            st.set("cond", "1");
            st.playSound("ItemSound.quest_accept");
            st.setState(State.STARTED);
		}
		else if (event.equalsIgnoreCase("31389-13.htm")) //11
		{
			st.set("cond", "3");
            st.playSound("ItemSound.quest_middle");
            st.giveItems(SilverCross, 1);
		}
		else if (event.equalsIgnoreCase("31389-18.htm")) //16
			st.playSound("InterfaceSound.charstat_open_01");
		else if (event.equalsIgnoreCase("31389-19.htm")) //17
		{
			st.takeItems(BrokenSilverCross, -1);
            st.giveItems(Hairpin, 1);
            st.set("cond", "5");
		}
		else if (event.equalsIgnoreCase("31522-03.htm")) //03
			st.takeItems(Totem, -1);
		else if (event.equalsIgnoreCase("31389-08.htm")) //7
			st.set("cond", "11");
		else if (event.equalsIgnoreCase("31522-21.htm")) //19
		{
			st.giveItems(SuspiciousTotem, 1);
            st.addExpAndSp(242105, 22529);
            st.exitQuest(false);
            st.playSound("ItemSound.quest_finish");
		}
		else if (event.equalsIgnoreCase("31531-02.htm")) //02
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "2");
            st.takeItems(Flower, -1);
		}
		else if (event.equalsIgnoreCase("31532-04.htm")) //04
		{
			st.playSound("ItemSound.quest_middle");
            st.giveItems(Letter, 1);
            st.set("cond", "6");
		}
		else if (event.equalsIgnoreCase("31532-06.htm")) //06
		{
			st.takeItems(Hairpin, -1);
            st.takeItems(Letter, -1);
		}
		else if (event.equalsIgnoreCase("31532-19.htm")) //16
		{
			st.playSound("ItemSound.quest_middle");
            st.set("cond", "9");
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = Quest.getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (st.isCompleted())
		{
			if (npc.getNpcId() == Wizard)
				htmltext = "31522-22.htm";
			else
			{
				htmltext = Quest.getAlreadyCompletedMsg(player);
				return htmltext;
			}
		}
		
		int cond = st.getInt("cond");
		
		switch (st.getState())
		{
		case State.CREATED:
			QuestState st2 = player.getQuestState("Q023_LidiasHeart");
			if (st2.isCompleted())
			{
				if (st.getPlayer().getLevel() >= 65)
					htmltext = "31389-01.htm";
				else
				{
					htmltext = "31389-02.htm";
					st.exitQuest(true);
				}
			}
			else
			{
				htmltext = "31389-02.htm";
				st.exitQuest(true);
			}
			break;
			
		case State.STARTED:
			switch (npc.getNpcId())
			{
			case Dorian:
				if (cond == 1)
					htmltext = "31389-04.htm"; //03
				else if (cond == 2)
					htmltext = "31389-05.htm"; //04
				else if (cond == 3)
					htmltext = "31389-14.htm"; //12
				else if (cond == 4)
					htmltext = "31389-15.htm"; //13
				else if (cond == 5)
					htmltext = "31389-20.htm"; //18
				break;
				
			case Tombstone:
				if (cond == 1)
				{
					st.playSound("AmdSound.d_wind_loot_02");
	                htmltext = "31531-01.htm"; //01
				}
				else if (cond == 2)
					htmltext = "31531-03.htm"; //03
				break;
				
			case MaidOfLidia:
				if (cond == 5)
					htmltext = "31532-01.htm"; //01
				else if (cond == 6)
				{
					if (st.getQuestItemsCount(Letter) == 1 && st.getQuestItemsCount(Hairpin) == 1)
						htmltext = "31532-05.htm"; //05
					else
						htmltext = "31532-07.htm"; //07
					
					//htmltext = "31532-07a.htm"; in case of the player doesn't have items and it must talk again uses this htm
				}
				else if (cond == 9)
					htmltext = "31532-19.htm"; //16
				break;
				
			case Wizard:
				if (cond == 10)
					htmltext = "31522-01.htm"; //01
				else if (cond == 11)
					htmltext = "31522-09.htm"; //08
				break;
			}
			break;
		}
		
		return htmltext;
	}
	
	@Override
	public String onKill(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return null;
		
		if (st.getInt("cond") == 9)
		{
			if (st.getQuestItemsCount(Totem) != 1)
			{
				if (st.getRandom(100) <= 30)
				{
					st.giveItems(Totem, 1);
	                st.set("cond", "10");
	                st.playSound("ItemSound.quest_middle");
				}
			}
		}
		
		return null;
	}
	
	@Override
	public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
	{
		L2Attackable monster = ((L2Attackable) npc);
		
		if (isPet)
			monster.getAggroList().remove(player.getPet());
		else
		{
			monster.getAggroList().remove(player);
			QuestState st = player.getQuestState(qn);
			
			if (st != null && st.getQuestItemsCount(SilverCross) == 1)
			{
				st.takeItems(SilverCross, -1);
				st.giveItems(BrokenSilverCross, 1);
				st.set("cond", "4");
				autoChat(npc,"That sign!");
			}
		}
		
		return null;
	}
	
	public static void main(String[] args)
	{
		new Q024_InhabitantsOfTheForestOfTheDead(24, qn, "Inhabitants Of The Forest Of Dead");
	}
}